package com.tansuo365.test1.util;

import org.apache.shiro.crypto.AesCipherService;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Component;

import java.security.Key;
import java.util.HashMap;
import java.util.Map;

/**
 * 机器码的加密
 */
@Component
public class EncryptUtil {

    public String encryptMD5BySalt(String machineCode) {
        if (machineCode.length() != 0) {
            String salt = "enjoy-your-bucket";
//            String algorithmName = "md5"; //md5轻易不可逆
            String MILING_FIRST = new SimpleHash("md5", machineCode, salt, 58).toString();

            String MILING_FINAL = encryptAES(MILING_FIRST);
            return MILING_FINAL;
        }
        return "";
    }

    /*二次加密*/
    private String encryptAES(String firstEncryption) {
        // AES算法实现：
        AesCipherService aesCipherService = new AesCipherService();
        aesCipherService.setKeySize(128); //设置key长度
        //生成key
//        Key key = aesCipherService.generateNewKey();
//        System.out.println("aesKey:"+key);

        String twinceKeySalt = "life-is-cool-jun";
        byte[] bytes = twinceKeySalt.getBytes();
        //加密
        String encrptText = aesCipherService.encrypt(firstEncryption.getBytes(), bytes).toHex();
        //解密
//        String text2 = new String(aesCipherService.decrypt(Hex.decode(encrptText), key.getEncoded()).getBytes());

        return encrptText;
    }
}
